package com.goods.controller.business;

import com.github.pagehelper.PageInfo;
import com.goods.business.service.*;
import com.goods.common.model.business.InStock;
import com.goods.common.model.business.OutStock;
import com.goods.common.response.ActiveUser;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：Hachimen
 * @date ：Created in 2021/9/25 8:59
 * @description：入库记录
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("/business/outStock")
public class OutStockWebController {

    @Autowired
    private OutStockService outStockService;
    @Autowired
    private ConsumerService consumerService;
    @Autowired
    private ProductService productService;
    //物资出库 分页查询
    @GetMapping("findOutStockList")
    public ResponseBean findOutStockList(HttpServletRequest request) {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        String status = request.getParameter("status");
        String type = request.getParameter("type");
        String outNum = request.getParameter("outNum");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        PageInfo<OutStockVO> page = outStockService.findOutStockList(pageNum,pageSize,status,type,outNum);
        Map<String, Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getList());
        return ResponseBean.success(data);
    }

    //物资出库 明细
    @GetMapping("detail/{id}")
    public ResponseBean detail(@PathVariable Long id,HttpServletRequest request){
        String pageNum = request.getParameter("pageNum");
        PageInfo<OutStockItemVO> page = outStockService.detail(id,pageNum);
        OutStock outStock= outStockService.findOneOutStock(id);
        ConsumerVO consumerVO = consumerService.edit(outStock.getConsumerId());
        Map<String, Object> data = new HashMap<>();
        data.put("itemVOS",page.getList());
        data.put("total",page.getTotal());
        data.put("consumerVO",consumerVO);
        data.put("status",outStock.getStatus());
        return ResponseBean.success(data);
    }

    //物资出库 回收站
    @PutMapping("remove/{id}")
    public ResponseBean remove(@PathVariable Long id){
        outStockService.remove(id);
        return ResponseBean.success();
    }

    //物资出库 还原
    @PutMapping("back/{id}")
    public ResponseBean back(@PathVariable Long id){
        outStockService.back(id);
        return ResponseBean.success();
    }

    //物资出库 审核
    @PutMapping("publish/{id}")
    public ResponseBean publish(@PathVariable Long id){
        outStockService.publish(id);
        return ResponseBean.success();
    }

    //物资出库 删除
    @GetMapping("delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        outStockService.delete(id);
        return ResponseBean.success();
    }

    //物资出库 添加
    @PostMapping("addOutStock")
    public ResponseBean addIntoStock(@RequestBody OutStockVO outStockVO){
        //获取用户名
        ActiveUser principal = (ActiveUser)SecurityUtils.getSubject().getPrincipal();
        String username = principal.getUser().getUsername();
        outStockService.add(outStockVO,username);
        return ResponseBean.success();
    }


}
